const http = require("http");
const https = require("https");
const fs = require("fs");
const getFans = require('./getFans')
const iconv = require('iconv-lite')
const users = require('./users.js')
const headers = require('./header.js');
const links = require('./links');
const moment = require('moment')
// 三天前的数据时间节点
const before3d = new Date(moment().add(-3, 'd')).getTime()
console.log(before3d)
http.createServer(async (request, response) => {
    
    const requestUrl = request.url;//客户端请求的路径
    const pathname = requestUrl.split("?")[0];

    if (pathname === "/fans") { //如果请求的是a就返回a的html
        console.log('粉丝数据')
        let index = 0;
        let result = ''
        users.map((v, i) => {
            setTimeout(function () {
                let name = v.name
                https.get(links.fans(v.id), {
                    "headers": headers,
                      "body": null,
                      "method": "GET"
                }, function (res) {
                    let chunks = []
                    res.on("data", function (chunk) {
                        chunks.push(chunk);
                    })
                    res.on('end', () => {
                            //将获取的代码整合在一起
                            var str = Buffer.concat(chunks);
                            //处理乱码
                            var data = iconv.decode(str, 'utf-8');
                            data = JSON.parse(data)
                            index++
                            let word = name + ':' + data.data.user.followers_count + '</br>'
                            result += word
                            if (index == users.length) {
                                console.log(result)
                                response.setHeader("Content-Type", 'text/html; charset=utf-8')
                                response.end(result);
                            }
                       
                    })
                })
            }, 400)
        })

    }

    if(pathname === '/diary'){
        console.log('每日数据')
        let index = 0;
        let result = ''
        users.map((v, i) => {
            setTimeout(function () {
                let name = v.name + ':' + '</br>'
                
                https.get(links.diary(v.uid), {
                    "headers": headers,
                      "body": null,
                      "method": "GET"
                }, function (res) {
                    let chunks = []
                    res.on("data", function (chunk) {
                        chunks.push(chunk);
                    })
                    res.on('end', () => {
                            //将获取的代码整合在一起
                            var str = Buffer.concat(chunks);
                            //处理乱码
                            var data = iconv.decode(str, 'utf-8');
                            data = JSON.parse(data)
                            index++
                            
                            let word = '';
                            for(let i = 0; i < data.data.list.length; i++){
                                let time = moment(new Date(data.data.list[i].created_at)).format('YYYY MM DD  hh:mm:ss')

                                let comments = data.data.list[i].comments_count
                                let attitudes_count = data.data.list[i].attitudes_count
                                let reposts_count = data.data.list[i].reposts_count
                                

                                let timestamp = new Date(time).getTime()
                                if(timestamp > before3d){
                                        word += `<h4>${time}</h4>`
                                        word += data.data.list[i].text_raw 
                                        word += '</br>'
                                        word += '转发:'+reposts_count +',评论:' + comments+',点赞:' + attitudes_count
                                }
                            }
                            result += `<h1>${name}</h1>`
                            result += word
                            if (index == users.length) {
                                console.log(result)
                                response.setHeader("Content-Type", 'text/html; charset=utf-8')
                                response.end(result);
                            }
                       
                    })
                })
            }, 400)
        })
    }

}).listen(80)